<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改结算单明细')"/>
</head>
<body class="gray-bg">
<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">客户姓名：</label>
                            <label class="col-sm-6 control-label">[[${statement.customerName}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">联系方式：</label>
                            <label class="col-sm-6 control-label">[[${statement.customerPhone}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">车牌号码：</label>
                            <label class="col-sm-6 control-label">[[${statement.licensePlate}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">汽车类型：</label>
                            <label class="col-sm-6 control-label">[[${statement.carSeries}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">服务类型：</label>
                            <label class="col-sm-6 control-label">[[${@dict.getLabel('si_service_catalog',statement.serviceType)}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">到店时间：</label>
                            <label class="col-sm-6 control-label"
                                   th:text="${#dates.format(statement.actualArrivalTime, 'yyyy-MM-dd HH:mm')}"></label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">总消费金额：</label>
                            <label class="col-sm-6 control-label"
                                   id="totalAmount">[[${statement.totalAmount}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">实付价格：</label>
                            <label class="col-sm-6 control-label" id="actuallyPayMoney">[[${statement.totalAmount-statement.discountAmount}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">优惠价格：</label>
                            <input class="col-sm-6" type="number" onchange="updateAmount()" name="discountAmount"
                                   th:field="${statement.discountAmount}"/>
                        </div>
                    </div>
                </div>
            </div>
            <div class="btn-group-sm" id="item-toolbar" role="group">
                <a id="saveBtn" class="btn btn-success disabled" onclick="saveFun()">
                    <i class="fa fa-plus"></i> 保存
                </a>
                <a id="payBtn" class="btn btn-warning " onclick="payFun()">
                    <i class="fa fa-paypal"></i> 支付
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="initStatementItemTable"></table>
            </div>
        </div>
    </div>
</div>

<div class="ui-layout-east">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="stateItemFormId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>名称：</label>
                                <input type="text" name="name" placeholder="输入优惠价格"/>
                            </li>
                            <li>
                                <label>是否套餐：</label>
                                <select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                            th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>服务分类：</label>
                                <select name="serviceCatalog"
                                        th:with="type=${@dict.getType('si_service_catalog')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                            th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="searchFun()"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="initServiceItemTable"></table>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: layout-latest"/>
<script th:inline="javascript">
    $('body').layout({
        east__size: '35%',
    });
    $(function () {
        initStatementItem();
        initServiceItem();
    });
    var statementId = [[${statement.id}]];

    function initStatementItem() {
        var options = {
            id: "initStatementItemTable",
            toolbar: 'item-toolbar', //工具栏
            url: "/business/statementItem/query?statementId=" + statementId,
            modalName: "结算单明细",
            showSearch: false, //显示搜索
            pagination: true,  //分页
            showRefresh: true, //显示刷新
            uniqueId: 'itemId',
            columns: [
                {
                    field: 'itemId',
                    title: '服务单ID',
                    visible: false
                },
                {
                    field: 'itemName',
                    title: '服务项名称'
                },
                {
                    field: 'itemPrice',
                    title: '服务项价格'
                },
                {
                    field: 'itemQuantity',
                    title: '购买数量'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="addFun(\'' + row.itemId + '\')"><i class="fa fa-plus"></i></a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="minusFun(\'' + row.itemId + '\')"><i class="fa fa-minus"></i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }

    function initServiceItem() {
        var options = {
            id: 'initServiceItemTable',
            url: "/business/serviceItem/query?saleStatus=1",
            pagination: true,
            uniqueId: 'id',
            columns: [
                {
                    field: 'name',
                    title: '服务项名称'
                },
                {
                    field: 'discountPrice',
                    title: '服务项价格'
                },
                {
                    field: 'info',
                    title: '备注信息',
                    formatter: function (value, row, index) {
                        return $.table.tooltip(value, 0);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" ' +
                            'onclick="addToStatementTable(\'' + row.id + '\')"><i class="fa fa-plus"></i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    }

    function addToStatementTable(id) {
        // 根据唯一Id获取行数据
        var rs = $('#initServiceItemTable').bootstrapTable('getRowByUniqueId', id);
        var row = $('#initStatementItemTable').bootstrapTable('getRowByUniqueId', id);
        // 判断这条数据是否在initStatementItemTable里面,如果不在就插入数据,在就更新数据
        if (row) {
            // 存在这条记录  根据唯一Id更新行数据
            $("#initStatementItemTable").bootstrapTable('updateByUniqueId', {id: id, row: {itemQuantity: row.itemQuantity + 1}});
        } else {
            // 不存在这条记录
            $('#initStatementItemTable').bootstrapTable('append', {
                itemId: rs.id,
                itemName: rs.name,
                itemPrice: rs.discountPrice,
                itemQuantity: 1
            });
        }
        updateAmount();
    }

    function addFun(id) {
        var row = $('#initStatementItemTable').bootstrapTable('getRowByUniqueId', id);
        $('#initStatementItemTable').bootstrapTable('updateByUniqueId', {
            id: id,
            row: {itemQuantity: row.itemQuantity + 1}
        });
        updateAmount();
    }

    function minusFun(id) {
        var row = $('#initStatementItemTable').bootstrapTable('getRowByUniqueId', id);
        if (row.itemQuantity > 1) {
            $('#initStatementItemTable').bootstrapTable('updateByUniqueId', {id: id, row: {itemQuantity: row.itemQuantity - 1}});
        } else {
            $('#initStatementItemTable').bootstrapTable('removeByUniqueId', id);
        }
        updateAmount();
    }

    function updateAmount() {
        var data = $('#initStatementItemTable').bootstrapTable('getData', true);
        var totalAmount = 0;
        $.each(data, function (index, ele) {
            totalAmount += ele.itemPrice * ele.itemQuantity;
        });
        $("#totalAmount").html(totalAmount);
        var discountAmount = $("#discountAmount").val();
        if (!discountAmount) {
            discountAmount = 0;
        }
        if (discountAmount < 0) {
            $("#discountAmount").val(0);
            discountAmount = 0;
        }
        if (discountAmount > totalAmount) {
            alert("输入优惠价格有误");
            $("#discountAmount").val(0);
            discountAmount = 0;
        }
        $("#actuallyPayMoney").html(totalAmount - discountAmount);
        $("#saveBtn").removeClass("disabled");

    }

    function saveFun() {
        var datas = $("#initStatementItemTable").bootstrapTable('getData', true);
        $.each(datas, function (index, ele) {
            ele.statementId = statementId;
        });
        // 携带额外的数据
        datas.push({itemPrice: $("#discountAmount").val(), statementId: statementId});
        console.log(datas);
        var config = {
            url: "/business/statementItem/saveItems",
            type: 'post',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify(datas),
            success: function (result) {
                $("#saveBtn").addClass("disabled");
                $.operate.ajaxSuccess(result);
            }
        };
        $.ajax(config)
    }

    function payFun() {
        $.modal.confirm("是否要支付",function () {
            $.get("/business/statementItem/pay?statementId="+statementId,function (result) {
                if(result.code == web_status.SUCCESS){
                    $.modal.confirm(result.msg,function () {
                        $.modal.closeTab();
                    });
                }else{
                    $.modal.alertError(result.msg);
                }
            })
        });
    }
</script>
</body>
</html>